package edu.aynu.service.impl;

import edu.aynu.dao.EsProblemDao;
import edu.aynu.entity.Problem;
import edu.aynu.dao.ProblemDao;
import edu.aynu.service.ProblemService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Problem)表服务实现类
 *
 * @author sitong
 * @date 2021-06-21 20:24:21
 */
@Service("problemService")
public class ProblemServiceImpl implements ProblemService {
    @Resource
    private ProblemDao problemDao;

    @Resource
    private EsProblemDao esProblemDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Problem queryById(Integer id) {
        return this.problemDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Problem> queryAllByLimit(int offset, int limit) {
        return this.problemDao.queryAllByLimit(offset, limit);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param problem 实例对象
     * @return 对象列表
     */
    @Override
    public List<Problem> queryAll(Problem problem) {
        return this.problemDao.queryAll(problem);
    }

    /**
     * 新增数据
     *
     * @param problem 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(Problem problem) {
        String title = problem.getTitle().trim();

        // 判断是否存在一样的题目
        Problem conditionProblem = new Problem();
        conditionProblem.setType(problem.getType());
        conditionProblem.setSubject(problem.getSubject());
        List<Problem> problems = this.problemDao.queryAll(conditionProblem);
        for (Problem tempPro : problems) {
            String sqlTitle = tempPro.getTitle().split("###")[0].trim();
            if(title.equals(sqlTitle)) {
                return 0;
            }
        }

        int i = problemDao.insert(problem);
        List<Problem> problemList = problemDao.queryAll(problem);
        esProblemDao.save(problemList.get(0));
        return i;
    }

    /**
     * 修改数据
     *
     * @param problem 实例对象
     * @return 实例对象
     */
    @Override
    public int update(Problem problem) {
        return this.problemDao.update(problem);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.problemDao.deleteById(id) > 0;
    }

    /**
     * 通过Es查询数据
     * @param title
     * @return
     */
    @Override
    public List<Problem> queryByEs(String title) {
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title", title);

        List<Problem> problems = new ArrayList<>();
        esProblemDao.search(matchQueryBuilder).forEach((problemTemp) -> {
            if(problems.size() < 5) {
                problems.add(problemTemp);
            }
        });

        return problems;
    }
}
